package spring.chatroom.java_chatroom.common.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import spring.chatroom.java_chatroom.common.constant.Constants;
import spring.chatroom.java_chatroom.common.handler.LoginInterceptor;
import spring.chatroom.java_chatroom.common.handler.WebSocketHandler;

import java.nio.channels.Channel;


@Configuration
@EnableWebSocket
public class WebConfig implements WebMvcConfigurer, WebSocketConfigurer {

    @Autowired
    LoginInterceptor l;

    @Autowired
    WebSocketHandler w;

    @Autowired
    Constants constants;

    //斜杆不能缺
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(l)
                .addPathPatterns("/chat/**")
                .excludePathPatterns("/chat/login")
                .excludePathPatterns("/chat/register")
                .excludePathPatterns("/settingInfo/**")
                .excludePathPatterns("/**/*.html", "/**/*.css", "/**/*.js", "/**/*.png");
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(w, "/WebSocketMessage")
                // 通过注册这个特定的 HttpSession 拦截器, 就可以把用户给 HttpSession 中添加的 Attribute 键值对
                // 往我们的 WebSocketSession 里也添加一份.
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }

    /**
     * 资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/chat-picture/**")
                .addResourceLocations("file:" + constants.localPicPath + "/");

        registry.addResourceHandler("/chat-video/**")
                .addResourceLocations("file:" + constants.localVideoPath + "/");

        registry.addResourceHandler("/chat-document/**")
                .addResourceLocations("file:" + constants.localDocPath + "/");

        registry.addResourceHandler("/chat-others/**")
                .addResourceLocations("file:" + constants.localOthersPath + "/");

        registry.addResourceHandler("/head-portrait/**")
                .addResourceLocations("file:" + constants.localPortraitPath + "/");

        registry.addResourceHandler("/chat-background/**")
                .addResourceLocations("file:" + constants.localBackgroundPath + "/");
    }


}
